package com.js.sys.fragment.view.forth;

import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.js.lib_common.base.MvpFragment;
import com.js.lib_common.event.StartBrotherEvent;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.presenter.ProductPresenter;
import com.js.lib_common.mvp.view.ProductView;
import com.js.sys.BaseApplication;
import com.js.sys.R;
import com.js.sys.adapter.BindingAdapter;
import com.js.sys.fragment.view.first.BindHistoryFragment;
import com.js.sys.greendao.db.BindingHistory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 出库
 * Created by wz on 2021/5/19.
 */
public class CheckOutFragment extends MvpFragment<ProductPresenter> implements ProductView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.rcy_list)
    RecyclerView rcyList;
    private BindingAdapter mAdapter;

    private static final int DEFAULT_VIEW = 0x22;

    private BindBean mBindBean;

    public static CheckOutFragment newInstance() {
        Bundle args = new Bundle();
        CheckOutFragment fragment = new CheckOutFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    protected ProductPresenter createPresenter() {
        return new ProductPresenter(this);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_forth_checkout;
    }


    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        toolbarTitle.setText(R.string.tab_forth);
        mAdapter = new BindingAdapter(_mActivity, null);
        initRecyclerView(rcyList, mAdapter, 1);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.im_del) {
                    mAdapter.getData().remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

        toolbar.inflateMenu(R.menu.menu_history);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                EventBus.getDefault().post(new StartBrotherEvent(BindHistoryFragment.newInstance(BindBean.REQUEST_CODE_CHECK_OUT)));
                return false;
            }
        });
    }

    /**
     * 接收扫码后的信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveQrcode(BindBean bean) {
        if (bean.getType() != BindBean.REQUEST_CODE_CHECK_OUT
                && bean.getType() != BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM) {
            return;
        }
        Vibrator vibrator = (Vibrator) _mActivity.getSystemService(Service.VIBRATOR_SERVICE);
        if (vibrator != null)
            vibrator.vibrate(300);
        if (mAdapter.getData().contains(bean)) {
            showTips("该项已存在", "请不要重复扫码");
            return;
        }
        if (bean.getType() == BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM) {
            mAdapter.addData(bean);
            return;
        }
        if (bean.getType() == BindBean.REQUEST_CODE_CHECK_OUT && bean.getQrcode().length() == 18) {
            mAdapter.addData(bean);
            return;
        }
        showTips("格式有误", "非整机ID");


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }



    @OnClick({R.id.rl_scan, R.id.rl_custom, R.id.rl_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_scan:
                scan(DEFAULT_VIEW, BindBean.REQUEST_CODE_CHECK_OUT);
                break;

            case R.id.rl_custom:
                scan(DEFAULT_VIEW, BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM);
                break;
            case R.id.rl_save:
                List<BindBean> list = mAdapter.getData();
                if (list.size() == 0)
                    return;
                String clientCode = "";
                for (BindBean bindBean : list) {
                    if (bindBean.getType() == BindBean.REQUEST_CODE_CHECK_OUT) {
                        mBindBean = bindBean;
                    } else {
                        clientCode = bindBean.getQrcode();
                    }
                }

                mvpPresenter.checkOut(BaseApplication.userId, mBindBean.getQrcode(), clientCode);

                break;
        }
    }


    @Override
    public void onSuccess() {
        BindingHistory.insertHistory(mBindBean, true, BindBean.REQUEST_CODE_CHECK_OUT, "onSuccess");
        showTips("出库成功", "");
    }

    @Override
    public void onFailed(String message) {
        showTips("出库失败", message);
        BindingHistory.insertHistory(mBindBean, false, BindBean.REQUEST_CODE_CHECK_OUT, message);
    }


}
